package com.ys.controller.web.business;


import java.util.HashMap;
import java.util.Map;

import com.ys.service.business.TpAplusSqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.controller.api.business.ApiInterTpAplusSqController;
import com.ys.entity.business.TpAplusSq;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/business/tpAplusSq")
public class WebTpAplusSqController {

    @Autowired
    private CommonApiService<TpAplusSq> commonApiService;

    @Autowired
    private TpAplusSqService tpAplusSqService;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        try {
            String bid = commonApiService.getCommonNextBid(ApiInterTpAplusSqController.MENU_ID1);

            return commonApiService.toAdd(model, new TpAplusSq().setBid(bid), ApiInterTpAplusSqController.MENU_ID1, null, null);

        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
//    @RequiresPermissions("tpAplusSq:edit")
    @GetMapping("/toEdit")
    public String toEdit(Model model, String bid) throws Exception{
        if (bid == null){
            return W.error(model, "单号不能为空");
        }

        Map<String, Object> entity = tpAplusSqService.getExtraParam(bid);
        if (entity == null){
            return W.error(model, "该A+申请已删除!");
        }

        String aa = commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new TpAplusSq(), ApiInterTpAplusSqController.MENU_ID1, entity, null);


        YsProcInst yspProcInst = new YsProcInst().setBid(bid).queryFirst();
        Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
        model.addAttribute("onProcess", onProcess);

        return aa;
    }
}
